package com.cskaoyan.service.wxfree;

import com.cskaoyan.bean.market.Keyword;
import com.cskaoyan.bean.market.KeywordExample;
import com.cskaoyan.bean.user.SearchHistory;
import com.cskaoyan.bean.user.SearchHistoryExample;
import com.cskaoyan.bean.user.WxUserLoginData;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import com.cskaoyan.vo.wxfree.WxSearchIndexVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-12 09:26
 **/
// 微信端搜索模块
@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    KeywordMapper keywordMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public WxSearchIndexVo searchIndex() {
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();

        // 查询所有默认关键词
        KeywordExample defaultK = new KeywordExample();
        KeywordExample.Criteria criteria = defaultK.createCriteria();
        criteria.andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        List<Keyword> defaultKeyword = keywordMapper.selectByExample(defaultK);

        // 随机一个默认的关键字
        int index = (int) (Math.random() * defaultKeyword.size());
        Keyword keyword = defaultKeyword.get(index);

        // 查询所有热门关键词
        KeywordExample hotK = new KeywordExample();
        KeywordExample.Criteria criteria1 = hotK.createCriteria();
        criteria1.andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<Keyword> hotKeyWord = keywordMapper.selectByExample(hotK);

        // 根据用户id 查询搜索历史的关键字
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria2 = searchHistoryExample.createCriteria();
        criteria2.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);

        // 类型转换
        ArrayList<WxSearchIndexVo.HistoryKeywordListBean> historyKeywordListBeans = new ArrayList<>();
        Converter converter = PojoUtils.converter();
        converter.listConverter(searchHistories, historyKeywordListBeans, WxSearchIndexVo.HistoryKeywordListBean.class);

        return new WxSearchIndexVo(keyword, hotKeyWord, historyKeywordListBeans);

    }

    @Override
    public List<String> searchHelper(String keyword) {
        // 模糊查询
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordLike("%" + keyword + "%").andDeletedEqualTo(false);
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        // 类型转换
//        String[] strings = new String[keywords.size()];
//        for (Keyword k : keywords) {
//            for (int i = 0; i < strings.length; i++) {
//                strings[i]=k.getKeyword();
//            }
//        }
        ArrayList<String> strings = new ArrayList<>();
        for (Keyword keyword1 : keywords) {
            strings.add(keyword1.getKeyword());
        }
        return strings;
    }

    @Override
    public void searchClearHistory() {
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();

        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        criteria.andUserIdEqualTo(userId);
        // 所以是先查询，后删除
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);

        // 逻辑删除该用户所有搜索历史
        for (SearchHistory searchHistory : searchHistories) {
            searchHistory.setDeleted(true);
            searchHistory.setUpdateTime(new Date());
            searchHistoryMapper.updateByPrimaryKeySelective(searchHistory);
        }
    }

    @Override
    public void insertHistory(String keyword) {
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        // TODO: 这里前台只有wx端，写死了wx
        String from = "wx";
        Date addTime = new Date();
        Date updateTime = new Date();
        // 判断关键字是否存在
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(userId)
                .andDeletedEqualTo(false)
                .andKeywordEqualTo(keyword);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        if (searchHistories.size() ==1) {
            SearchHistory searchHistory = searchHistories.get(0);
            searchHistory.setUpdateTime(updateTime);
            searchHistoryMapper.updateByPrimaryKeySelective(searchHistory);
        }else {
            SearchHistory searchHistory = new SearchHistory(null, userId, keyword, from, addTime,updateTime,false);
            searchHistoryMapper.insertSelective(searchHistory);
        }
    }
}
